package com.szholly.pro.watergas_server.ps.android.common;

import com.szholly.data.general.Field;


import com.szholly.data.general.Field;
import com.szholly.data.general.GlobalDataType;
import com.szholly.plug.form.validation.FieldIsNullException;
import com.szholly.plug.form.validation.StringLengthException;


import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 字段校验拦截器
 *
 * 2017/3/23 <br/>
 *
 * @author Joey Huang QQ:273179370）
 */
public interface FieldInterceptor {
	void interceptor(Field field, String value) throws Exception;

	// 获取不为空校验器
	public static FieldInterceptor createNotNullInterceptor(String... notAllowNullFieldArray) {
		return new FieldNotNullInterceptor(notAllowNullFieldArray);
	}

	// 校验
	public static void validateField(Field[] fields, HashMap<String, Object> rowBase, List<FieldInterceptor> interceptorList) throws Exception {
		if (interceptorList == null || interceptorList.size() == 0) {
			return;
		}
		ValidUtils.notEmpty(fields);
		ValidUtils.notEmpty(rowBase);
		for (Field field : fields) {
			String fieldName = field.getFieldName();
			Object valueObject = rowBase.get(fieldName);
			String value = valueObject == null ? "" : valueObject.toString();
			for (FieldInterceptor fieldInterceptor : interceptorList) {
				fieldInterceptor.interceptor(field, value);
			}
		}
	}

	// 校验
	public static void validateField(Field[] fields, HashMap<String, Object> rowBase, FieldInterceptor... interceptorArray) throws Exception {
		if (interceptorArray == null || interceptorArray.length == 0) {
			return;
		}
		List<FieldInterceptor> interceptorList = Arrays.asList(interceptorArray);
		validateField(fields, rowBase, interceptorList);
	}

	// 字段长度校验器
	public class FieldLengthInterceptor implements FieldInterceptor {
		@Override
		public void interceptor(Field field, String value) throws Exception {
			if (value == null || value.length() == 0 || "null".equals(value)) {
				return;
			}

			GlobalDataType dataType = field.getDataType();
			if (dataType != GlobalDataType.String) {
				return;
			}

			String str = value.replaceAll("[^\\x00-\\xff]", "**");
			int maxLen = field.getMaxLen();
			if (str.length() > maxLen) {
				throw new StringLengthException(field.getFieldAliasName() + " 长度过长！");
			}
		}
	}

	// 字段空校验器
	public class FieldNullInterceptor implements FieldInterceptor {
		@Override
		public void interceptor(Field field, String value) throws Exception {
			String fieldName = field.getFieldName();
			if ("GUID".equals(fieldName)) {// GUID会自动生成，跳过
				return;
			}

			if (value != null && value.length() > 0) {// 值不为空，跳过
				return;
			}
			if (field.getAllowNull()) {// 字段允许为空
				return;
			}
			throw new FieldIsNullException(field.getFieldAliasName() + "不能为空！");
		}
	}

	// 字段强制不为空校验器
	public class FieldNotNullInterceptor implements FieldInterceptor {
		private List<String> notAllowNullFieldList;

		public FieldNotNullInterceptor(List<String> notAllowNullFieldList) {
			this.notAllowNullFieldList = notAllowNullFieldList;
		}

		public FieldNotNullInterceptor(String... notAllowNullFieldArray) {
			if (notAllowNullFieldArray != null && notAllowNullFieldArray.length > 0) {
				this.notAllowNullFieldList = Arrays.asList(notAllowNullFieldArray);
			}
		}

		@Override
		public void interceptor(Field field, String value) throws Exception {
			if (value != null && value.length() > 0) {// 值不为空，跳过
				return;
			}
			if (notAllowNullFieldList != null && notAllowNullFieldList.contains(field.getFieldName())) {// 强制不为空
				throw new FieldIsNullException(field.getFieldAliasName() + "不能为空！");
			}
		}
	}

}
